package com.wdl.modules.myhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.dao.CommentsDao;
import com.wdl.modules.myhouse.entity.CommentsEntity;
import com.wdl.modules.myhouse.service.CommentsService;
import com.wdl.modules.myhouse.vo.CommentsVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * 评论表
 *
 * @author lucain sunlightcs@gmail.com
 * @since 1.0.0 2022-10-06
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsDao, CommentsEntity> implements CommentsService {


    @Override
    public List<CommentsVo> getComments(Long fileId) {
        List<CommentsVo> comments = this.baseMapper.allRootCommentByFileId(fileId);
        List<CommentsVo> all = this.baseMapper.findAllByFileId(fileId);
        for (CommentsVo root : comments) {
            fillTheChild(root, all);
        }
        return comments;
    }

    @Override
    public List<CommentsVo> getLastestComment(Integer num) {
        return this.baseMapper.findCommentByCommentTimeDesc(num);
    }

    private void fillTheChild(CommentsVo node, List<CommentsVo> commentsVos) {
        Long id = node.getCommentId();
        for (CommentsVo vo : commentsVos) {
            if (vo.getCommentParentId().equals(id)) {
                fillTheChild(vo, commentsVos);
                node.getChildComments().add(vo);
            }
        }
    }
}